package com.eii.eiimq.mqtask;

import com.eii.eiimq.config.RabbitMQConfig;
import com.eii.eiimq.service.RecvmsgTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Auther: eii
 * @Date: 2020/7/11 13:52
 * @Description:
 */
@Component
public class RecvmsgTask {

    Logger logger = LoggerFactory.getLogger(RecvmsgTask.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RecvmsgTaskService recvmsgTaskService;

    @RabbitListener(queues = RabbitMQConfig.EIIMQ_SEND_MSG_QUEUE)
    public void receiveMsgTask(String msg){
        //接收到msg
        logger.info("消费者 接收消息:{}", msg);
        boolean result = recvmsgTaskService.processMsg(msg);

        if(result){
            //发回给生产者，处理完成消息，生产者根据此消息进行后续业务处理
            String backMsg = "finished processing " + msg;
            rabbitTemplate.convertAndSend(RabbitMQConfig.EX_EIIMQ_SEND_MSG,RabbitMQConfig.EIIMQ_RECV_MSG_KEY,backMsg);
            logger.info("消费者 发送消息:{}", backMsg);
        }
    }


}
